package com.luna.servlets;

import com.luna.beans.Note;
import com.luna.beans.User;
import com.luna.service.NoteService;
import com.luna.serviceImpl.NoteServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(
        name = "NoteAddition",
        urlPatterns = "/NoteAddition"
)
public class NoteAddition extends HttpServlet {

    public NoteAddition() {}

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int book_id = Integer.parseInt(req.getParameter("book_id"));
        int uploader_id = ((User) req.getSession().getAttribute("user")).getId();
        NoteService noteService = new NoteServiceImpl();
        PrintWriter printWriter = resp.getWriter();
        Note note = new Note(
                req.getParameter("note-title"),
                req.getParameter("note-content"),
                uploader_id, book_id
        );
        System.out.println(note);
        if (noteService.addNote(note)) {
            printWriter.print("true");
        } else {
            printWriter.print("false");
        }
        printWriter.close();
    }
}
